package com.appspot.yusupova;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.yusupova.verses.VerseMap;
import com.google.appengine.repackaged.com.google.common.util.Base64;

/**
 * <p>This servlet returns all the verse indexes stored in JDO.
 *  
 * <p>This servlet was very helpful to check indexes and find some
 * mistakes in my simplest search algorithm.
 * 
 * @author Rouslan Shchoutchinov
 */
@SuppressWarnings("serial")
public class GetAllIndexesServlet extends AdminHttpServlet {

    /**
     * Returns all indexes as a Base64 string
     * 
     * @param req    client's request
     * @param resp   server's response
     * @param writer writer to save all indexes
     * 
     * @throws IOException if it can't convert indexes to UTF-8 string 
     */
    protected void doAuthorizedPost(HttpServletRequest req,
            HttpServletResponse resp, PrintWriter writer) throws IOException {
        String allIndexes = VerseMap.getAllIndexes();
        String encoded = Base64.encode(allIndexes.getBytes("UTF-8"));
        writer.println(encoded);
    }

}
